using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace XnaUtility.Camera
{
    public class KeyboardCamera : CameraBase
    {
        #region "Properties"
        private Control.InputBase input;

        private Keys upKey;
        private Keys downKey;
        private Keys leftKey;
        private Keys rightKey;
        private Keys forwardKey;
        private Keys backKey;
        private Keys pitchUp;
        private Keys pitchDown;
        private Keys yawLeft;
        private Keys yawRight;
        private Keys rollLeft;
        private Keys rollRight;
        private float moveSpeed;
        private float rotateSpeed;

        public float RotateSpeed
        {
            get { return rotateSpeed; }
            set { rotateSpeed = value; }
        }
	
        public float MoveSpeed
        {
            get { return moveSpeed; }
            set { moveSpeed = value; }
        }
	
        public Keys RollRight
        {
            get { return rollRight; }
            set { rollRight = value; }
        }
	
        public Keys Left
        {
            get { return rollLeft; }
            set { rollLeft = value; }
        }
	
        public Keys YawRight
        {
            get { return yawRight; }
            set { yawRight = value; }
        }
	
        public Keys YawLeft
        {
            get { return yawLeft; }
            set { yawLeft = value; }
        }
	
        public Keys PitchDown
        {
            get { return pitchDown; }
            set { pitchDown = value; }
        }
	
        public Keys PitchUp
        {
            get { return pitchUp; }
            set { pitchUp = value; }
        }
	
        public Keys BackKey
        {
            get { return backKey; }
            set { backKey = value; }
        }
	
        public Keys ForwardKey
        {
            get { return forwardKey; }
            set { forwardKey = value; }
        }
	
        public Keys RightKey
        {
            get { return rightKey; }
            set { rightKey = value; }
        }
	
        public Keys LeftKey
        {
            get { return leftKey; }
            set { leftKey = value; }
        }
	
        public Keys DownKey
        {
            get { return downKey; }
            set { downKey = value; }
        }
	
        public Keys UpKey
        {
            get { return upKey; }
            set { upKey = value; }
        }
	
        #endregion

        public KeyboardCamera(Game game, float fov, float ar)
            : base(game, fov, ar)
        {
           //default keys
            forwardKey = Keys.W;
            backKey = Keys.S;
            leftKey = Keys.A;
            rightKey = Keys.D;
            yawLeft = Keys.Q;
            yawRight = Keys.E;
            upKey = Keys.X;
            downKey = Keys.Z;
            rollLeft = Keys.F;
            rollRight = Keys.G;

            moveSpeed = 50;
            rotateSpeed = 60;
        }

        public KeyboardCamera(Game game, float fov, float ar, float near, float far)
            : base(game, fov, ar, near, far)
        {
            //default keys
            forwardKey = Keys.W;
            backKey = Keys.S;
            leftKey = Keys.A;
            rightKey = Keys.D;
            yawLeft = Keys.Q;
            yawRight = Keys.E;
            upKey = Keys.X;
            downKey = Keys.Z;
            rollLeft = Keys.F;
            rollRight = Keys.G;

            moveSpeed = 50;
            rotateSpeed = 60;
        }

        public override void Initialize()
        {
            input = (XnaUtility.Control.InputBase)Game.Services.GetService(typeof(Control.InputBase));

            if (input == null) throw new Exception("KeyboardCamera failed to get input service");

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
       {

            float seconds = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;

            if (input.IsKeyDown(upKey))
            {
                this.Location.Y += moveSpeed * seconds;
            }
            if (input.IsKeyDown(downKey))
            {
                this.Location.Y -= moveSpeed * seconds;
            }

            if (input.IsKeyDown(rightKey))
            {
                this.Location.X += moveSpeed * seconds;
            }
            if (input.IsKeyDown(leftKey))
            {
                this.Location.X -= moveSpeed * seconds;
            }

            if (input.IsKeyDown(forwardKey))
            {
                this.Location.Z += moveSpeed * seconds;
            }
            if (input.IsKeyDown(backKey))
            {
                this.Location.Z -= moveSpeed * seconds;
            }


            base.Update(gameTime);
        }
    }
}
